const UserModel = require("../../models/UserModel")
const UserService = require("../../services/admin/UserService")
const JWT = require("../../util/JWT")
const { path } = require("../../util/path.config")

const UserController = {
    login: async (req, res) => {
        var result = await UserService.login(req.body)
        if (result.length !== 0) {
            const token = JWT.generate({
                _id: result[0]._id
            }, "1d")
            res.header("Authorization", token)
            res.send({
                message: "登录成功",
                code: 200,
                data: result[0]
            })
        } else {
            res.send({
                message: "用户名或密码不匹配",
                code: 500
            })
        }
    },
    detail: async (req, res) => {
        console.log(req.body);
        var result = await UserService.detail(req.body._id)
        console.log(result);
        if (result.length !== 0) {
            res.send({
                message: "查找成功",
                code: 200,
                data: result[0]
            })
        } else {
            res.send({
                message: "用户不存在",
                code: 500
            })
        }
    },
    update: async (req, res) => {
        await UserService.update(req.body)
        res.send({
            code: 200,
            message: "修改成功",
            data: req.body
        })
    },
    add: async (req, res) => {
        await UserService.add(req.body)
        res.send({
            code: 200,
            message: "添加成功"
        })
    },
    list:async (req,res)=>{
       let result  = await UserService.list(req.query)
        // console.log(result);
        res.send({
            code:200,
            message:"查询成功",
            data:result
        })
    },
    delete:async (req,res)=>{
        // console.log(req.params);
        let result = await UserService.delete(req.params.id)
        res.send({
            code:200,
            message:"删除成功"
        })
    }
}

module.exports = UserController